import Vue from 'vue';
const host = 'https://tmmwm.cross.echosite.cn/yuepu/'
const apiUrl = host + 'api/';
const apiRount = {
	'hot': 0,
	'details':0,
	'search':0,
	'search_total':0,
	'gsk':'get_search_keyword',
	'download_total':0
}
const url = function(u, param) {
	let uri = apiUrl + (!apiRount[u] ? u:apiRount[u]);
	if (param) {
		let k;
		let paramArray = [];
		for (k in param) {
			paramArray.push(k + '=' + param[k]);
		}
		uri += '?' + paramArray.join('&');
	}
	return uri;
}

const alert = function(title, icon, duration) {
	uni.showToast({
		icon: icon ? icon : 'none',
		title: title,
		duration: duration ? duration : 1500
	});
}

const setStorage = function(key, data) {
	try {
		uni.setStorageSync(key, data);
	} catch (e) {
		//TODO handle the exception
	}
}
const getStorage = function(key) {
	try {
		const data = uni.getStorageSync(key);
		return data;
	} catch (e) {
		//TODO handle the exception
	}
}
const headerInfo = {
	'Content-Type': 'application/x-www-form-urlencoded',
	'X-Requested-With': 'XMLHttpRequest'
}

const navigateTo = function(obj){
	uni.navigateTo(obj);
}

const reLaunch = function(obj){
    uni.reLaunch(obj);
}

export default {
	host,
	apiUrl,
	url,
	alert,
	headerInfo,
	getStorage,
	setStorage,
	navigateTo,
	reLaunch
}
